package com.leemon.mall.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.leemon.mall.common.util.CacheManagerUtil;
import com.leemon.mall.security.constants.SecurityConstants;
import com.leemon.mall.security.dto.MySysUser;
import com.leemon.mall.sys.dao.SysUserRoleMapper;
import com.leemon.mall.sys.model.SysUser;
import com.leemon.mall.sys.dao.SysUserMapper;
import com.leemon.mall.sys.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author leemon
 * @since 2019-09-04
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private CacheManagerUtil cacheManagerUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(cacheNames = "sys_user", key = "#userId")
    public SysUser updatePasswordByUserId(Long userId, String password) {
        SysUser sysUser = sysUserMapper.selectById(userId);
        sysUser.setPassword(password);
        sysUserMapper.updateById(sysUser);

        //更新密码时，删除缓存中的userDetails信息(用户登录时会首先调用缓存获取用户数据，如果不删除缓存，会导致用户修改的新密码与缓存的密码不一致从而使登录失败)
        cacheManagerUtil.evictCache(SecurityConstants.SECURITY_SYS_USER, sysUser.getUsername());
        return sysUser;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserAndUserRole(SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUserMapper.insert(sysUser);
        if (CollUtil.isEmpty(sysUser.getRoleIdList())) {
            return;
        }
        //保存用户及对应的角色关系
        sysUserRoleMapper.insertUserAndUserRole(sysUser.getUserId(), sysUser.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys_user", key = "#sysUser.userId")
    public void updateUserAndUserRole(SysUser sysUser) {
        //更新用户
        sysUserMapper.updateById(sysUser);

        //先删除用户对应的角色关系
        sysUserRoleMapper.deleteByUserId(sysUser.getUserId());
        if (CollUtil.isEmpty(sysUser.getRoleIdList())) {
            return;
        }
        //保存用户及对应的角色关系
        sysUserRoleMapper.insertUserAndUserRole(sysUser.getUserId(), sysUser.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] userIds, Long shopId) {
        sysUserMapper.deleteBatch(userIds, shopId);
    }

    @Override
    public SysUser getByUserName(String username) {
        return sysUserMapper.selectByUserName(username);
    }

    @Override
    @Cacheable(cacheNames = "sys_user", key = "#userId")
    public SysUser getByUserId(Long userId) {
        return sysUserMapper.selectById(userId);
    }

    @Override
    @CacheEvict(cacheNames = "sys_user", key = "#userId")
    public void evictSysUserById(Long userId) {

    }
}
